#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/12 07:40
@file: chinaz_lazy_loading.py
@desc: 图片懒加载爬取
"""

import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/92.0.4515.159 Safari/537.36',
}


def get_content(page):
    page_flag = '' if page == 1 else f'_{page}'
    url = f'https://sc.chinaz.com/tupian/meinvtupian{page_flag}.html'
    page = requests.get(url=url, headers=headers)
    page.encoding = 'utf-8'
    return page.text


def download_img(req):
    tree = etree.HTML(req)
    img_list = tree.xpath('//*[@id="container"]//div/a/img')
    for img in img_list:
        pname = img.xpath('./@alt')[0] + '.jpg'
        # 这里注意懒加载, 刷新页面 src -> src2
        psrc = 'https:' + img.xpath('./@src2')[0]
        content = requests.get(url=psrc, headers=headers).content
        # 当前文件夹下创建imgs目录
        with open(f'imgs/{pname}', 'wb') as fp:
            fp.write(content)
        print(f'下载{pname}完成')


def run():
    start = int(input('请输入开始页面:'))
    end = int(input('请输入结束页面:'))
    for p in range(start, end + 1):
        request = get_content(p)
        download_img(request)


if __name__ == '__main__':
    run()
